files = dir('*.tif');
files.name
%%
for k = 1:numel(files)
    im = imread(files(k).name);
    im = im(:,:,1:3);

    im2 = im(:,:,1);

    h  = im2 > 200;
    m  = im2 < 200 & im2 > 50;
    l  = im2 < 50;

    im2(h) = 255;
    im2(m) = 136;
    im2(l) = 0;

    [~,filename] = fileparts(files(k).name);
    fid = fopen([filename '.coe'],'w');
    for i = 1:size(im2,1)
        str = '';
        for j = 1:size(im2,2)
            switch (im2(i,j))
                case 199 
                    str = [str '0,'];
                case 136
                    str = [str '0,'];
                case 0
                    str = [str '2,'];
                case 255
                    str = [str '3,'];
            end
        end
        fprintf(fid,'%s\n',str);
    end
    fclose(fid)
    image(im2); colormap(gray)
end